//
//  AnotherViewController.m
//  09-UITableView省市区
//
//  Created by LL.F on 16/7/26.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "AnotherViewController.h"
#define kScreenbounds [UIScreen mainScreen].bounds
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight  [UIScreen mainScreen].bounds.size.height
@interface AnotherViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITableView *zoneTableView;
@property (nonatomic, strong) NSMutableArray *areaArray;
@property (nonatomic, strong) NSMutableArray *zoneArray;
@end

@implementation AnotherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"两层省市区";
    self.view.backgroundColor = [UIColor colorWithRed:0.327 green:0.817 blue:0.791 alpha:1.000];
    self.areaArray = [NSMutableArray array];
    self.zoneArray = [NSMutableArray array];
    
    // 数据解析
    NSString *string = [NSString stringWithContentsOfFile:@"/Users/LL.F/DLS160710/UI/HomeWork/09-UITableView省市区/09-UITableView省市区/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *array = [string componentsSeparatedByString:@"\n"];
    //NSLog(@"%@", array);
    for (NSString *str in array) {
        if (![str hasPrefix:@"  "]) {
            NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
            [provinceDic setObject:str forKey:@"proName"];
            NSMutableArray *cityArray = [NSMutableArray array];
            [provinceDic setObject:cityArray forKey:@"cityArray"];
            // NSLog(@"%@", provinceDic);
            [self.areaArray addObject:provinceDic];
            
        } else if (![str hasPrefix:@"    "]) {
        
            // 获取到当前市的省
            NSMutableDictionary *provinceDic = [self.areaArray lastObject];
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:str forKey:@"cityName"];
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDic setObject:zoneArray forKey:@"zoneArray"];
            NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
            [cityArray addObject:cityDic];
        } else {
        
            NSMutableDictionary *provinceDic = [self.areaArray lastObject];
            NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
            NSMutableDictionary *cityDic = [cityArray lastObject];
            NSMutableArray *zoneArray = [cityDic objectForKey:@"zoneArray"];
            [zoneArray addObject:str];
            }
    }
   
    //NSLog(@"%@", self.areaArray);
    
    //初始化tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth / 2, kScreenHeight - 49) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    // 初始化zoneTableView
    self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth / 2, 0, kScreenWidth / 2, kScreenHeight - 49) style:UITableViewStylePlain];
    self.zoneTableView.delegate = self;
    self.zoneTableView.dataSource = self;
    [self.view addSubview:_zoneTableView];
    [self.zoneTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"zoneCell"];
    
                          
    
    
}
#pragma mark - UITableView Datasource
// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (tableView == _tableView) {
         return [[[self.areaArray objectAtIndex:section] objectForKey:@"cityArray"] count];
    } else
    {
    
       return  self.zoneArray.count;
    }
   
    
}
// cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSMutableDictionary *provinceDic = [self.areaArray objectAtIndex:indexPath.section];
    NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
    NSMutableDictionary *cityDic = [cityArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [cityDic objectForKey:@"cityName"];
        return cell;

    } else  {
    
        UITableViewCell *cell = [self.zoneTableView dequeueReusableCellWithIdentifier:@"zoneCell"];
        cell.textLabel.text = [self.zoneArray objectAtIndex:indexPath.row];
        return cell;
    }
    
}
// 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    if (tableView == _tableView) {
        return self.areaArray.count;
    } else {
    
        return 1;
    }
}
// 设置分区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

    if (tableView == _tableView) {
        return [[self.areaArray objectAtIndex:section] valueForKey:@"proName"];
    } else
    {
    
        return nil;
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -- UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    if (tableView == _tableView) {
        
        NSMutableDictionary *proDic = [self.areaArray objectAtIndex:indexPath.section];
        NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
        NSMutableDictionary *cityDic = [cityArray objectAtIndex:indexPath.row];
        self.zoneArray = [cityDic objectForKey:@"zoneArray"];
        [self.zoneTableView reloadData];
    }
    

    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
